package com.example.beikeyigou.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.beikeyigou.R;
import com.example.beikeyigou.custom.AmountView;

public class BuyNowDialog extends Dialog {
    private AmountView amountView;
    private ImageView closeImg, goodImg;
    private TextView goodPrice, remainNum;
    private Button confirmBtn;

    public AmountView getAmountView() {
        return amountView;
    }

    public void setAmountView(AmountView amountView) {
        this.amountView = amountView;
    }

    public ImageView getGoodImg() {
        return goodImg;
    }

    public BuyNowDialog setGoodImg(ImageView goodImg) {
        this.goodImg = goodImg;
        return this;
    }

    public TextView getGoodPrice() {
        return goodPrice;
    }

    public BuyNowDialog setGoodPrice(String goodPrice) {
        this.goodPrice.setText(goodPrice);
        return this;
    }

    public TextView getRemainNum() {
        return remainNum;
    }

    public BuyNowDialog setRemainNum(String remainNum) {
        this.remainNum.setText(remainNum);
        return this;
    }

    public BuyNowDialog(@NonNull Context context) {
        super(context);
    }

    // 接口回调
    public interface OnEnsureListener {
        // 确定
        public void onEnsure(int num);
    }
    OnEnsureListener onEnsureListener;
    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_buy_now_btn:
                    if (onEnsureListener!= null) {
                        onEnsureListener.onEnsure(amountView.getNum());
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_buy_now);
        setDialogWidth();

        findView();
        setListener();
    }

    private void findView() {
        closeImg = findViewById(R.id.dialog_buy_now_cancel);
        amountView = findViewById(R.id.dialog_buy_now_amount);
        goodImg = findViewById(R.id.dialog_buy_now_good);
        goodPrice = findViewById(R.id.dialog_buy_now_price);
        remainNum = findViewById(R.id.dialog_buy_now_remain);
        confirmBtn = findViewById(R.id.dialog_buy_now_btn);
    }

    private void setListener() {
        closeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        confirmBtn.setOnClickListener(clickListener);
    }

    // 设置对话框宽度和屏幕宽度一致
    public void setDialogWidth(){
        Window window = getWindow();   //当前屏幕窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();  //获取窗口信息参数
        // 获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int)(d.getWidth());   //对话框窗口宽度为屏幕窗口宽度
        wlp.gravity = Gravity.BOTTOM;    //从底部弹出对话框
        window.setBackgroundDrawableResource(android.R.color.transparent);   //设置窗口背景透明
        window.setAttributes(wlp);
        // 自动弹出软键盘
        // handler.sendEmptyMessageDelayed(1,100);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };

    // 设置剩余库存
    public BuyNowDialog setStorage_num(int num) {
        amountView.setGoods_storage(num);
        return this;
    }
}
